package com.minelx.sequence.pattern.variable;

import com.minelx.sequence.pattern.config.IReducer;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ReducedVariableManager implements IVariableManager {
	private final VariableManager variableManager;

	private final Map<String, IReducer<?>> reducers;

	public ReducedVariableManager(Map<String, IReducer<?>> reducers) {
		variableManager = new VariableManager();
		this.reducers = reducers;
	}

	@Override
	public void set(String key, Object value) {
		if (!reducers.containsKey(key)) {
			variableManager.set(key, value);
			return;
		}
		IReducer<?> reducer = reducers.get(key);
		variableManager.set(key, reducer.reduce(((List<?>) value)));
	}

	@Override
	public Object get(String key) {
		return variableManager.get(key);
	}

	@Override
	public Consumer<Object> setterOf(String variableName) {
		return variableManager.setterOf(variableName);
	}

	@Override
	public String toString() {
		return "ReducedVariableManager{" +
				"variableManager=" + variableManager +
				", reducers=" + reducers +
				'}';
	}
}
